package com.service.lossservice.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.controller.vo.LossVO;
import com.mapper.CarinsuranceidMapper;
import com.mapper.CaseoneMapper;
import com.mapper.LossMapper;
import com.mapper.PolicyMapper;
import com.pojo.Carinsuranceid;
import com.pojo.CarinsuranceidExample;
import com.pojo.CarinsuranceidExample.Criteria;
import com.pojo.Caseone;
import com.pojo.CaseoneExample;
import com.pojo.Loss;
import com.pojo.LossExample;
import com.pojo.Policy;
import com.pojo.PolicyExample;
import com.service.lossservice.Lossservice;
@Service
public class LossserviceImpl implements Lossservice{
    @Autowired
    private LossMapper lossMapper;
    @Autowired
    private CarinsuranceidMapper carinsuranceidMapper;
	@Autowired
	private PolicyMapper policyMapper;
	@Autowired
	private CaseoneMapper caseoneMapper;
	
	
	@Override
	public List<LossVO> getLossVO(String lossType, String lossName) {
		List<LossVO> listVO=new ArrayList<LossVO>();  
		
		if (lossType.equals("0")) {
			List<Loss> list = lossMapper.selectByExample(null);
			if (list!=null&&list.size()>0) {
				for (int i = 0; i < list.size(); i++) {
					  LossVO lossVO = new LossVO();
                      Loss loss = list.get(i);
                      Integer lossid = loss.getLossid();
                      String caseno = loss.getCaseno();
                      CaseoneExample caseoneExample = new CaseoneExample();
                      com.pojo.CaseoneExample.Criteria criteria3 = caseoneExample.createCriteria();
                      criteria3.andCasenoEqualTo(caseno);
                      List<Caseone> list4 = caseoneMapper.selectByExample(caseoneExample);
                      Caseone caseone=null;
                      if (list4!=null&&list4.size()>0) {
					   caseone = list4.get(0);
					  }
                      String policyno = loss.getPolicyno();
                      PolicyExample policyExample = new PolicyExample();
                      com.pojo.PolicyExample.Criteria criteria2 = policyExample.createCriteria();
                      criteria2.andPolicynoEqualTo(policyno);
                      List<Policy> list3 = policyMapper.selectByExample(policyExample);
                      Policy policy=null;
                      if (list3!=null&&list3.size()>0) {
						policy = list3.get(0);
					  }
                      
                      CarinsuranceidExample carinsuranceidExample = new CarinsuranceidExample();
                      Criteria criteria = carinsuranceidExample.createCriteria();
                      criteria.andPolicynoEqualTo(policyno);
                      List<Carinsuranceid> list2 = carinsuranceidMapper.selectByExample(carinsuranceidExample);
                      Carinsuranceid carinsuranceid=null;
                      if (list2!=null&&list2.size()>0) {
						 carinsuranceid = list2.get(0);
					  }
                      lossVO.setCaseone(caseone);
                      lossVO.setLossid(lossid);
                      lossVO.setPolicy(policy);
                      lossVO.setCarinsuranceid(carinsuranceid);
                      listVO.add(lossVO);
					
					
				}
			}
			
			
			
		} else if(lossType.equals("1")){
           
			String vin  =  lossName;
			
			CarinsuranceidExample carinsuranceidExample = new CarinsuranceidExample();
			Criteria criteria = carinsuranceidExample.createCriteria();
			criteria.andVinEqualTo(vin);
			List<Carinsuranceid> list = carinsuranceidMapper.selectByExample(carinsuranceidExample);
			Carinsuranceid carinsuranceid=null;
			if (list!=null&&list.size()>0) {
			 carinsuranceid = list.get(0);
			}
			String policyno = carinsuranceid.getPolicyno();
			LossExample lossExample = new LossExample();
			com.pojo.LossExample.Criteria criteria2 = lossExample.createCriteria();
			criteria2.andPolicynoEqualTo(policyno);
			List<Loss> list2 = lossMapper.selectByExample(lossExample);
			if (list2!=null&&list2.size()>0) {
				for (int j = 0; j < list2.size(); j++) {
					  LossVO lossVO = new LossVO();
                      Loss loss = list2.get(j);
                      String caseno = loss.getCaseno();
                      CaseoneExample caseoneExample = new CaseoneExample();
                      com.pojo.CaseoneExample.Criteria criteria3 = caseoneExample.createCriteria();
                      criteria3.andCasenoEqualTo(caseno);
                      List<Caseone> list4 = caseoneMapper.selectByExample(caseoneExample);
                      Caseone caseone=null;
                      if (list4!=null&&list4.size()>0) {
					   caseone = list4.get(0);
					  }
                      Integer lossid = loss.getLossid();
                      String policyno1 = loss.getPolicyno();
                      PolicyExample policyExample = new PolicyExample();
                      com.pojo.PolicyExample.Criteria criteria4 = policyExample.createCriteria();
                      criteria4.andPolicynoEqualTo(policyno1);
                      List<Policy> list3 = policyMapper.selectByExample(policyExample);
                      Policy policy=null;
                      if (list3!=null&&list3.size()>0) {
						policy = list3.get(0);
					  }
                      
                      CarinsuranceidExample carinsuranceidExample1 = new CarinsuranceidExample();
                      Criteria criteria5 = carinsuranceidExample1.createCriteria();
                      criteria5.andPolicynoEqualTo(policyno);
                      List<Carinsuranceid> list5 = carinsuranceidMapper.selectByExample(carinsuranceidExample);
                      Carinsuranceid carinsuranceid1=null;
                      if (list5!=null&&list5.size()>0) {
						 carinsuranceid1 = list5.get(0);
					  }
                      lossVO.setCaseone(caseone);
                      lossVO.setLossid(lossid);
                      lossVO.setPolicy(policy);
                      lossVO.setCarinsuranceid(carinsuranceid1);
                      listVO.add(lossVO);
					
				}
				
				
			}
			
			
			
		}else if(lossType.equals("2")){
			
			 String caseno = lossName;
			 LossExample lossExample = new LossExample();
			 com.pojo.LossExample.Criteria criteria = lossExample.createCriteria();
			 criteria.andCasenoEqualTo(caseno);
			 List<Loss> list = lossMapper.selectByExample(lossExample);
			 if (list!=null&&list.size()>0) {
				for (int i = 0; i < list.size(); i++) {
					 LossVO lossVO = new LossVO();
                     Loss loss = list.get(i);
                     String caseno1 = loss.getCaseno();
                     CaseoneExample caseoneExample = new CaseoneExample();
                     com.pojo.CaseoneExample.Criteria criteria3 = caseoneExample.createCriteria();
                     criteria3.andCasenoEqualTo(caseno1);
                     List<Caseone> list4 = caseoneMapper.selectByExample(caseoneExample);
                     Caseone caseone=null;
                     if (list4!=null&&list4.size()>0) {
					   caseone = list4.get(0);
					  }
                     Integer lossid = loss.getLossid();
                     String policyno = loss.getPolicyno();
                     PolicyExample policyExample = new PolicyExample();
                     com.pojo.PolicyExample.Criteria criteria2 = policyExample.createCriteria();
                     criteria2.andPolicynoEqualTo(policyno);
                     List<Policy> list3 = policyMapper.selectByExample(policyExample);
                     Policy policy=null;
                     if (list3!=null&&list3.size()>0) {
						policy = list3.get(0);
					  }
                     
                     CarinsuranceidExample carinsuranceidExample = new CarinsuranceidExample();
                     Criteria criteria6 = carinsuranceidExample.createCriteria();
                     criteria6.andPolicynoEqualTo(policyno);
                     List<Carinsuranceid> list2 = carinsuranceidMapper.selectByExample(carinsuranceidExample);
                     Carinsuranceid carinsuranceid=null;
                     if (list2!=null&&list2.size()>0) {
						 carinsuranceid = list2.get(0);
					  }
                     lossVO.setCaseone(caseone);
                     lossVO.setLossid(lossid);
                     lossVO.setPolicy(policy);
                     lossVO.setCarinsuranceid(carinsuranceid);
                     listVO.add(lossVO);
				}
			}
		};
		
		
		
		
		
		return listVO;
	}

	@Override
	public int insert(Loss record) {
		
		return lossMapper.insert(record);
	}

	@Override
	public Loss selectByPrimaryKey(Integer lossid) {
		
		return lossMapper.selectByPrimaryKey(lossid);
	}

	@Override
	public LossVO getLossVO(Loss loss) {
		LossVO lossVO = new LossVO();
		lossVO.setLoss(loss);
		
		String policyno = loss.getPolicyno();
		PolicyExample policyExample = new PolicyExample();
		com.pojo.PolicyExample.Criteria criteria = policyExample.createCriteria();
		criteria.andPolicynoEqualTo(policyno);
		List<Policy> list = policyMapper.selectByExample(policyExample);
		Policy policy=null;
		if (list!=null&&list.size()>0) {
		   policy = list.get(0);
		}
		lossVO.setPolicy(policy);
		CarinsuranceidExample carinsuranceidExample = new CarinsuranceidExample();
		Criteria criteria2 = carinsuranceidExample.createCriteria();
		criteria2.andPolicynoEqualTo(policyno);
		List<Carinsuranceid> list2 = carinsuranceidMapper.selectByExample(carinsuranceidExample);
		Carinsuranceid carinsuranceid=null;
		if (list2!=null&&list2.size()>0) {
			 carinsuranceid = list2.get(0);
		}
		lossVO.setCarinsuranceid(carinsuranceid);
		
		
		
		return lossVO;
	}

	@Override
	public int updateByPrimaryKey(Loss record) {
		
		return lossMapper.updateByPrimaryKey(record);
	}



}
